/*
 * 用户行为数据搜索
 * @Author: along
 * @Date: 2020-03-30
 */
<template>
    <div class="container userSearch">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-box-content">
                    <p class="header-box-content-title">日期:</p>
                    <el-date-picker
                        v-model="head.date"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="small"
                        @change="getTableData(1)"
                    />
                </div>
                <div class="header-box-content">
                    <p class="header-box-content-title">数据筛选:</p>
                    <el-select
                        v-model="head.event"
                        placeholder="请选择"
                        size="small"
                        clearable
                        @change="getTableData(1)"
                        @clear="head.event = 0"
                    >
                        <el-option
                            v-for="item in eventType"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-box-content">
                    <el-select
                        v-model="head.compare"
                        placeholder="请选择"
                        size="small"
                        clearable
                        @change="getTableData(1)"
                        style="width:100px"
                    >
                        <el-option
                            v-for="item in compareType"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-box-content">
                    <el-input
                        v-model="head.number"
                        :placeholder="tips"
                        v-enterNumber
                        size="small"
                        clearable
                        type="number"
                        v-if="is_number"
                        @input="getTableData(1)"
                    />
                    <el-input
                        v-model="head.number"
                        :placeholder="tips"
                        size="small"
                        clearable
                        v-else
                        @input="getTableData(1)"
                    />
                </div>
            </div>
            <div class="header-right">
                <div class="header-box-content">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnReset()"
                    >
                        重置
                    </el-button>
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnSearch()"
                    >
                        搜索
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="userSearch"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="student_no"
                label="用户唯一编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="school_name"
                label="学校名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="behavior_value"
                label="数据"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="phone"
                label="手机号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnGetData(scope.row)"
                    >
                        查看
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import {getDate} from '@/assets/js/utils';
export default {
    mixins: [back],
    data () {
        return {
            is_number: true,//是否填写正整数
            head: {
                date: [
                    this.getTempDate(0),
                    this.getTempDate(1) - 1
                ],
                event: '',
                compare: 3,
                number: 1
            },
            tips: '只能填写正整数',
            compareType: [{
                value: 1,
                label: '>'
            },{
                value: 3,
                label: '>='
            },{
                value: 2,
                label: '=='
            },{
                value: 4,
                label: '<'
            },{
                value: 6,
                label: '<='
            }],
            eventType:[{
                value: 'a',
                label: '请选择'
            },{
                value: 'visit_app',
                label: '访问APP次数'
            },{
                value: 'visit_shop',
                label: '访问商城页面次数'
            },{
                value: 'order_count',
                label: '除0元购购买次数'
            },{
                value: 'order_money',
                label: '累计花销金额'
            },{
                value: 'search_goods',
                label: '商城产生搜索行为'
            },{
                value: 'click_search_goods',
                label: '搜索结果页点击商品'
            },{
                value: 'visit_goods',
                label: '浏览商品详情页'
            },{
                value: 'order_refund',
                label: '退款次数'
            }],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    directives: {
        enterNumber: {
            inserted (el, binding, vnode, oldVnode) {
                el.addEventListener("keypress",e => {
                    /* eslint-disable */
                    e = e || window.event;
                    let charcode = typeof e.charCode === 'number' ? e.charCode : e.keyCode;
                    let re = /\d/;
                    if(!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey){
                        if(e.preventDefault){
                            e.preventDefault();
                        }else{
                            e.returnValue = false;
                        }
                    }
                });
            }
        }
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '用户行为数据搜索'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();

        console.log(this.head.date, 'dd');
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/statistic_social%5Cget_behavior_student_list', {
                page_id: page || this.page.now_page,
                start_time: getDate(new Date(this.head.date[0])),
                end_time: new Date(this.head.date[1]).getTime() / 1000 + 86300,
                behavior_name: this.head.event,
                op_type: this.head.compare,
                behavior_value: this.head.number
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 重置
         */
        fnReset () {
            this.head = {
                date: [
                    this.getTempDate(0),
                    this.getTempDate(1) - 1
                ],
                event: '',
                compare: 3,
                number: 1
            }
            this.tableData = [];
        },

        /**
         * 搜索
         */
        fnSearch () {
            if (this.head.event == 0) {
                this.$message({
                    type: 'message',
                    message: '请先选择事件',
                    center: true
                })
                return;
            }
            this.getTableData(1);
        },

        /**
         * 查看详情
         */
        fnGetData (row) {
            this.$router.replace({
                name: 'TaskUserList',
                query: {
                    student_no: row.student_no
                }
            });
        },

        /**
         * 获取指定日期0点时间戳
         */
        getTempDate (day) {
            let tempDate = new Date();
            let retult_milliseconds = tempDate.getTime() + 1000 * 60 * 60 * 24 * day;

            tempDate.setTime(retult_milliseconds);

            let result_Year = tempDate.getFullYear(),
                result_Month = (tempDate.getMonth() + 1).toString(),
                result_Date = tempDate.getDate().toString();

            if (result_Month.length === 1) {
                result_Month = "0" + result_Month;
            }

            if (result_Date.length === 1) {
                result_Date = "0" + result_Date;
            }

            let date = result_Year + "-" + result_Month + "-" + result_Date + " " + "00:00:00";

            return new Date(date).getTime();
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-box-content {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .userSearch .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .userSearch .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .userSearch .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .userSearch .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>